﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace HuntWump2
{
    class HighScore
    {

        public static int check = 0;

        //return a list of all top scores as a string array
        public static string[] getScores()
        {

            string[] scorez = new string[20];

            if (File.Exists("HighScores.txt") == false)
            {
                return null;
            }
            else
            {
                StreamReader scanz = new StreamReader("HighScores.txt");

                string str = "";
                for (int i = 0; i < 19; i += 2)
                {
                    str = scanz.ReadLine();
                    scorez[i] = str.Split('\t').ElementAt(0);
                    scorez[i + 1] = str.Split('\t').ElementAt(1);
                }
                scanz.Close();
                return scorez;
            }
        }//end getScores


        //insert a new high score, returning 0 if successful, else -1
        public static int addHighScore(String name, int score)
        {
            if (File.Exists("NewHighScores.txt"))
            {
                File.Delete("NewHighScores.txt");
            }

            StreamWriter printor = new StreamWriter("NewHighScores.txt");

            if (File.Exists("HighScores.txt") == false)
            {
                StreamWriter firstWrite = new StreamWriter("HighScores.txt");

                for (int i = 1; i <= 10; i++)
                {
                    printLine("name", 0, firstWrite);
                }
                firstWrite.Close();
            }

            StreamReader scanny = new StreamReader("HighScores.txt");

            for (int i = 1; i <= 10; i++)
            {
                String str = scanny.ReadLine();
                processLine(str, name, score, printor);
            }

            printor.Close();
            scanny.Close();
            File.Replace("NewHighScores.txt", "HighScores.txt", "HighScoresBackup.txt");

            return check;
        }//end addHighScore


        //Split line, compare to score
        //If applicable, insert high score and reduce repeat by one
        public static void processLine(String str, String name, int score, StreamWriter printor)
        {
            String player = str.Split('\t').ElementAt(0);
            int oldscore = Int32.Parse(str.Split('\t').ElementAt(1));
            if (oldscore <= score && check == 0)
            {
                check -= 1;
                printLine(name, score, printor);
            }

            printLine(player, oldscore, printor);
        }//end processLine

        public static void printLine(String name, int score, StreamWriter printor)
        {
            if (name.Length >= 8)
            {
                printor.Write(name.Substring(0, 7) + "\t" + score);
            }
            else
            {
                printor.Write(name + "\t" + score);
            }


            printor.WriteLine();
        }//end printLine

    }//end HighScores
}
